// 所有tab2代码都是第二个tab或者更多tab的代码参考
var validateForm;
function doSubmit() {
    return validateForm.form() ? ($("#inputForm").submit(), !0) : !1
}

$(document).ready(function () {
    validateForm = $("#inputForm").validate({
        ignore: "",
        submitHandler: function (a) {
            $("input[type=checkbox]").each(function () {
                $(this).after('<input type="hidden" name="' + $(this).attr("name") + '" value="' + ($(this).attr("checked") ? "1" : "0") + '"/>');
            });
            a.submit();
        },
        errorContainer: "#messageBox",
        errorPlacement: function (a, b) {
            $("#messageBox").removeClass("hide").text("\u8f93\u5165\u6709\u8bef\uff0c\u8bf7\u5148\u66f4\u6b63\u3002");
            b.is(":checkbox") || b.is(":radio") || b.parent().is(".input-append") ? a.appendTo(b.parent().parent()) : a.insertAfter(b)
        }
    });
});

var SpecValue = {
		imageInput:undefined,
		init:function(){
			var self= this;
			
			$("#addBtn").click(function(){
				self.addRow();
			});
			
			$("#delBtn").click(function(){
				self.deleteRow();
			});
			
			$("input[type='radio'][name='specType'][value=1]").click(function(){
				$("span[id='specImg']").children().show();
			});
			$("input[type='radio'][name='specType'][value=0]").click(function(){
				$("span[id='specImg']").children().hide();
			});
			
			this.bindEvent();
			this.resetColumnNo();
		},
		bindEvent:function(){
			var self = this;
			$("#tab-1 #contentTable1 tbody tr .delete").click(function(){
				var tr = $(this).parents("tr");
				var id = tr.find("input[name$=id]").val();
		        if(id){
		        	confirm("确认要删除吗？", function(result){
		            	if(result){
		            		ajaxReq(path + "/product/specification/deleteChild?id=" + id, function(){
		            			tr.remove(); //刷新列表
		            		});
		            	}
		            });
		        } else {
		        	tr.remove();
		        }
				self.resetColumnNo();
			});
			
			$(document).on('click', '.upload', function(e) {
		    	self.imageInput = $(this).siblings("input[type='hidden']");
		    	console.log(self.imageInput);
		    	$(this).siblings("input[type='file']").click();
		    	/*
				$("#imageModal").modal({
					show:true,
					keyboard: true
				});
				*/
		    });
			
			$(document).on("change", "input[type='file'][name='specImgFile']", function(e) {
				var filePath = $(this).val();
		        var fileTypes = "gif,jpg,jpeg,bmp,png" ;
		        var fileType = filePath.substring(filePath.lastIndexOf(".")+1,filePath.length);
		        //设置上传文件类型
		        if (fileTypes.indexOf(fileType) != -1) {
		        	//上传文件
		            $.ajaxFileUpload({
		                url: path + '/product/specValues/upload',
		                data:{
		                	subFolder: 'spec'
		                },
		                secureuri: false,
		                fileElementId: 'btnfile',
		                dataType: 'json',
		                success: function(data, status) {
		                    //获取上传文件路径
		                    if(data.success){
		                    	var src = data.message ;
		                    	var show = path + src ;
		                    	if(src.indexOf("http") == 0){
		                    		show = src ;
		                    	}
		                    	self.imageInput.parent().children("img.spec_pic").attr("src", show);
	    	            		self.imageInput.val(src);
		                    } else {
		                    	alert(data.message);
		                    }
		                },
		                error: function(data, status, e) {
		                    alert("上传出错："+e);
		                }
		            });
		        } else {
		            alert("请选择正确的文件格式！");
		            return false;
		        }
			});
			/*
			$("#uploadForm").submit(function () {
                 $(this).ajaxSubmit({
     	            type: "post",
     	            dataType:"json",
    	            success: function(data) {
    	            	debugger
    	                if(data.success){
    	                	self.imageInput.parent().children("img.spec_pic").attr("src",path + data.message);
    	            		self.imageInput.val(data.message);
    	            		$("#imageModal").modal('hide');
    	                }else{
    	                	alert("没有需要上传的文件!");
    	                }
    	            },
    	            error: function(XMLHttpRequest, textStatus, errorThrown){
    	            	alert("上传出错: " + errorThrown);
    	            },
    	            resetForm: true
    	        });
                return false;
             });
			*/
		},
		addRow:function(){
			var self = this;
			var tpl = $("#template1").clone();
		    tpl.removeAttr("id");
		    if($("input[type='radio'][name='specType']:checked").val() != 1){
				tpl.find("span[id='specImg']").children().hide();
			}
		    $("#tab-1 #contentTable1 tbody").append(tpl)
		    .find("td .delete").click(function(){
				$(this).parents("tr").remove();
				self.resetColumnNo();
			});
		    this.resetColumnNo();
		},
		deleteRow:function(){
			var idArr = new Array();
			var trArr = new Array();
			$("input[name='ck']:checked").closest("tr").each(function () {
		        var id = $(this).find("input[name='ck']").val();
		        var $this = $(this);
		        if(id){
		        	idArr.push(id);
		        	trArr.push($this);
		        } else {
		        	$this.remove();
		        }
		    });			
			if(idArr.length > 0){
				confirm("确认要删除吗？", function(result){
	            	if(result){
	            		$.each(idArr,function(i,id){
	            			ajaxReq(path + "/product/specification/deleteChild?id=" + id, function(){
	            				trArr[i].remove(); //刷新列表
	                		});
	        			});
	            	}
	            });
			}
			this.resetColumnNo();
		},
		resetColumnNo:function(){
			$("#tab-1 #contentTable1 tbody tr").each(function (b, c) {
		        $(this).find("input[name*=specValuesList]").each(function () {
		            var a = $(this).attr("name"), c = a.split(".")[1], c = "specValuesList[" + b + "]." + c;
		            $(this).attr("name", c);
		        });
		    });
		}
}
